<?php namespace App\Http\Controllers\Api\Udesk;

use App\Http\Controllers\Controller;
use App\Http\Services\FileManager;
use App\Merchant;
use App\User;
use App\UserInfo;
use Illuminate\Http\Request;

class UdeskController extends Controller
{
    public function getUserInfoByWxOpenId(Request $request){
        $wxOpenId = $request->get("wx_open_id");
        $merchantId = $request->get("merchant_id");

        $thatUser = User::whereWxOpenid($wxOpenId)->first();
        if(!$thatUser){
            return response()->json(["level"=>"error","message"=>"用户不存在"]);
        }

        $thatUserInfo = UserInfo::whereUserId($thatUser->id)->first();
        if(!$thatUserInfo){
            return response()->json(["level"=>"error","message"=>"用户信息不存在"]);
        }

        $thatMerchant = Merchant::whereId($merchantId)->first();

        $merchantName = "-";
        if($thatMerchant){
            $merchantName = $thatMerchant->name;
        }

        $userInfo = [
            "nickname"  => $thatUserInfo->nickname,
            "photo_url" => FileManager::getImgUrl($thatUserInfo->photo_id),
            "sex"       => $thatUserInfo->sex == 1 ? "男" : ($thatUserInfo->sex == 2 ? "女" : "未知"),
            "phone"     => !is_null($thatUser->phone) ? $thatUser->phone : "无",
            "merchant_name" => $merchantName
        ];

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$userInfo]);
    }
}
